<?php

/**
 * @name: HOTN AnimeList
 * @author: HammerOnTheNail
 * @link: http://code.google.com/p/hotnanimelist/
 */

class form_handler
{
	function form_handler()
	{
		return true;
	}
	
	/**
	 * 
	 * @param $data - Form Array
	 * @return String - Form HTML text 
	 */
	function show_form($data = array())
	{
		
		// Form kezdő
		$type = "";
		if ($data['type'] == "file_form") $type = "enctype='multipart/form-data'";
		$text = "
			<form action='".$data['action']."' method='".$data['method']."'".$type.">
				<table class='fborder' style='width: 95%;'>
		";
		
		// Form rejtett elemek
		foreach ($data['hidden'] as $key => $value)
		{
			$text .= "<input type='hidden' name='".$key."' value='".$value."' />";
		}
		
		// Form elemek
		foreach ($data['data'] as $form_data)
		{
			if ($form_data['show'] !== FALSE)
			{
				// Cím kiírása
				if ($form_data['type'] <> "submit"  && $form_data['type'] <> "form_title")
				{
					$text .= "
						<tr>
							<td style='width: 200px; vertical-align: top;' class='forumheader3'>
								".$form_data['text']."
							</td>
							<td class='forumheader3'>
					";
				}
				else
				{
					$text .= "
						<tr>
							<td colspan='2' class='forumheader' style='text-align: center;'>
	
					";
				}
			
				// Sima szöveg (cím) 
				if ($form_data['type'] == "form_title" || $form_data['type'] == "title")
				{
					$text .= $form_data['value'];
				}
				
				// Submit gomb
				if ($form_data['type'] == "submit")
				{
					$text .= "<input type='submit' class='button' name='".$form_data['name']."' id='".$form_data['name']."' value='".$form_data['value']."' />";
				}
				
				// Dátum mező
				if ($form_data['type'] == "date")
				{
					$text .= "<input type='text' class='tbox' size='76' value='".$form_data['value']."' maxlength='".$form_data['max']."' id='".$form_data['name']."' name='".$form_data['name']."' />";	
				}
			
				// Text mező
				if ($form_data['type'] == "text")
				{
					$text .= "<input type='text' class='tbox' size='76' value='".$form_data['value']."' maxlength='".$form_data['max']."' id='".$form_data['name']."' name='".$form_data['name']."' />";	
				}
				
				// Többsoros textarea
				if ($form_data['type'] == "textarea")
				{
					// sorok számának meghatározása
					if (!isset($form_data['rows'])) $form_data['rows'] = "9";
		
					// BBCode használata
					$bbcode_data = "";
					$bbcode_end = "";
					if ($form_data['bbcode'] === TRUE)
					{
						$bbcode_data = "onkeyup='storeCaret(this);' onclick='storeCaret(this);' onselect='storeCaret(this);'";
						require_once(e_HANDLER."ren_help.php");
						$bbcode_end = "<br />
						<input type='text' maxlength='100' size='90' name='help' class='helpbox' id='help' />
						<br />".display_help('help',$form_data['name']);	
					}
					
					$text .= "<textarea name='".$form_data['name']."' id='".$form_data['name']."' rows='".$form_data['rows']."' cols='74' class='tbox' ".$bbcode_data.">".$form_data['value']."</textarea>".$bbcode_end;
				}
				
				// Select mező
				if ($form_data['type'] == "select")
				{
					// Select mező és alap érték kiírása
					$selected = "";
					if ($form_data['selected'] == "none") $selected = "selected='selected'";
					$text .= "<select class='tbox' name='".$form_data['name']."' id='".$form_data['name']."'>
					<option value='none' ".$selected.">".HOTN_ANIMELIST_ADMIN_FORM_SELECTONE."</option>
					";
					
					// Mezők kiírása
					if ($form_data['group'] == "true")
					{
						// Csoportosított mezők
						foreach ($form_data['value'] as $key => $value)
						{
							$text .= "<optgroup label='".$key."'>";
								foreach ($value as $key2 => $value)
								{
									$selected = "";
									if ($form_data['selected'] == $key2) $selected = "selected='selected'";
									$text .= "<option value='".$key2."' ".$selected.">".$value."</option>";
								}
							$text .= "</optgroup>";
						}
					}
					else
					{
						// Nem csoportosított mezők
						foreach ($form_data['value'] as $key => $value)
						{
							$selected = "";
							if ($form_data['selected'] == $key) $selected = "selected='selected'";
							$text .= "<option value='".$key."' ".$selected.">".$value."</option>";
						}
					}
					
					$text .= "</select>";
				}
				
				// Választó doboz
				if ($form_data['type'] == "checkbox")
				{
					// Oszlopok meghatározása
					$row = $form_data['row'];
					
					// Dobozok kiírása meghatározott mennyiségű oszlopban
					$text .= "<table width='100%'>
						<tr>";
					foreach ($form_data['value'] as $key => $value)
					{
						$selected = "";
						if ($form_data['selected'][$key] == "1") $selected = "checked";
						$text .= "<td>
							<input type='checkbox' value='".$key."' name='".$form_data['name']."' ".$selected."/>
							".$value."
						</td>";
						if ( --$row <= 0)
						{
							$row = $form_data['row'];
							$text .= "</tr>
								<tr>
							";
						}
					}
					$text .= "
						</tr>
					</table>";
				}
				
				// Képfeltöltő, elözőleg feltöltött kép megjelenítésével
				if ($form_data['type'] == "image")
				{
					if ($form_data['value'] <> "")
					{
						$text .= "<img src='".$form_data['value']."' style='width:100px' /><br/>";
					}
					$text .= "<input class='tbox' type='file' name='".$form_data['name']."' />";
					if ($form_data['value'] <> "")
					{
						$text .= "<br /><input type='checkbox' value='true' name='delete_".$form_data['image_name']."' ".$selected."/>".HOTN_ANIMELIST_DEFAULT_FORM_DELETE;
					}
				}
				
				$text .= "
						</td>
					</tr>
				";
			}
		}
		
		$text .= "
			</table>
		</form>";
		return $text;
	}
}

$HOTN_ANIMELIST_FORM = new form_handler();
?>